﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Extension
{
    public static class Extend
    {
        public static IEnumerable<List<T>> Partition<T>(this IList<T> source, Int32 size)
        {
            for (int i = 0; i < Math.Ceiling(source.Count / (Double) size); i++)
                yield return new List<T>(source.Skip(size * i).Take(size));
        }
        
        public static string List2String<T>(this IList<T> source)
        {
            StringBuilder stringBuilder = new StringBuilder();
            T last = source.Last();
            stringBuilder.Append("[");
            for (int i = 0;i<source.Count;i++)
            {
                stringBuilder.Append(source[i]);
                if (i < source.Count)
                {
                    stringBuilder.Append(",");
                }
            }
            if (stringBuilder.Length > 0)
            {
                stringBuilder.Remove(stringBuilder.Length - 1, 1);
            }
            stringBuilder.Append("]");
            return stringBuilder.ToString();
        }
    }
}